package com.object.module.cbb.frontEnd;

import com.object.configuration.utils.Q;
import com.object.module.cbb.entity.CbbShoppingCartEntity;
import com.object.module.cbb.service.CbbShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * 购物车
 *
 * @author 小🌤
 * @email 1578442339@qq.com
 * @date 2022-02-07 14:10:44
 */
@RestController
@Slf4j
@RequestMapping("/frontend/shoppingcart")
public class FronyEndCbbShoppingCartController {
    @Autowired
    private CbbShoppingCartService cbbShoppingCartService;

    /**
     * 列表
     */
    @GetMapping("/all/{urId}")
    public Q list(@PathVariable("urId") Integer urId) {
        List<CbbShoppingCartEntity> list = cbbShoppingCartService.all(urId);
        return Q.ok().put("data", list);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public Q save(@RequestBody CbbShoppingCartEntity cbbShoppingCart) {
        boolean flag = cbbShoppingCartService.userAddsToCart(cbbShoppingCart.getScUrId(), cbbShoppingCart.getScSpId(), cbbShoppingCart);
        return flag ? Q.ok() : Q.error();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public void delete(@RequestBody Integer[] scIds) {
        cbbShoppingCartService.removeByIds(Arrays.asList(scIds));
    }

    /**
     * info 查询
     */
    @GetMapping("/info/{caId}")
    public CbbShoppingCartEntity info(@PathVariable("caId") Integer caId) {
        return cbbShoppingCartService.getById(caId);

    }


    /**
     * 修改
     */
    @PostMapping("/update")
    public boolean update(@RequestBody CbbShoppingCartEntity shoppingCartEntity) {
        return cbbShoppingCartService.updateById(shoppingCartEntity);
    }

}
